package com.wnxy.es;

import com.wnxy.es.entity.MovieInfo;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest
public class App {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    //1. 往索引库添加数据
    @Test
    public void add() {
        MovieInfo movieInfo = new MovieInfo();
        movieInfo.setMovieName("人生地不熟");
        movieInfo.setScore(8.5D);
        movieInfo.setMovieType("悬疑 / 喜剧");
        movieInfo.setTotalTime(180);
        movieInfo.setShowLocation("中国大陆");
        movieInfo.setDirector("易小星");
        movieInfo.setActor("乔杉 / 范丞丞 / 马丽 / 张婧仪");
        movieInfo.setIntroduce("车队大佬周东海（乔杉 饰）和老婆霍梅梅（马丽 饰）阴差阳错地与女儿周微雨（张婧仪 饰）及女儿的男朋友万一帆（范丞丞 饰）同行自驾，踏上人生路不熟的探亲之旅，周东海决定借此机会给准女婿来一场全方位无死角的考察，万一帆也用生命演绎了什么叫做教科书级翻车。一路上先后遭遇周东海死对头“添堵专业户”贾主任（田雨 饰）的房车事故、微雨“青梅竹马”光子（常远 饰）的“野鸡”山庄醉酒闹剧、捉拿公路贼团“油耗子”的围堵大战，鸡飞狗跳的探亲之旅窘态百出，爆笑不断！");
        elasticsearchRestTemplate.save(movieInfo);
    }

    //2. 主键查询
    @Test
    public void findById() {
        MovieInfo movieInfo = elasticsearchRestTemplate.get("1", MovieInfo.class);
        System.out.println("movieInfo = " + movieInfo);
    }

    //4. 条件查询
    @Test
    public void findByCondition() {
        // 构造条件
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        // term 词条查询，查询条件不会分词，作为一个整体匹配词条
        //builder.withQuery(QueryBuilders.termQuery("movieName","地不"));
        //builder.withQuery(QueryBuilders.matchQuery("movieName","人生如戏"));
        builder.withQuery(QueryBuilders.multiMatchQuery("大佬","movieName","introduce"));

        // 条件搜索
        SearchHits<MovieInfo> hits = elasticsearchRestTemplate.search(builder.build(), MovieInfo.class);

        long totalHits = hits.getTotalHits();
        System.out.println("查询命中总数：" + totalHits);

        List<SearchHit<MovieInfo>> searchHits = hits.getSearchHits();
        searchHits.stream().forEach(searchHit -> {
            MovieInfo content = searchHit.getContent();
            System.out.println("content = " + content);
        });
    }


    //4. 分页查询
    @Test
    public void page() {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.matchQuery("movieName","人生"));
        builder.withPageable(PageRequest.of(0,2));

        SearchHits<MovieInfo> search = elasticsearchRestTemplate.search(builder.build(), MovieInfo.class);
        List<MovieInfo> movieInfoList = search.getSearchHits().stream()
                .map(SearchHit::getContent).collect(Collectors.toList());
        movieInfoList.forEach(System.out::println);
    }
}
